#include "mmpf_i2cm.h"
#include "flash_ctl.h"
#include "hdm_ctl.h"
#include "mmpf_dram.h"

#if SUPPORT_FLASH_LIGHT


#if BIND_FLASH_TPS61050
void FLASH_TPS61050_Init(void);
void FLASH_TPS61050_FlashModeOn(MMP_UBYTE level);
void FLASH_TPS61050_FlashModeOff(void);
void FLASH_TPS61050_TorchModeOn(MMP_UBYTE level);
void FLASH_TPS61050_TorchModeOff(void);
#endif

#if BIND_FLASH_LM3554
void FLASH_LM3554_Init(void);
void FLASH_LM3554_FlashModeOn(MMP_UBYTE level);
void FLASH_LM3554_FlashModeOff(void);
void FLASH_LM3554_TorchModeOn(MMP_UBYTE level);
void FLASH_LM3554_TorchModeOff(void);
#endif


#define FLASH_CTL_ON			ON

#define FLASH_I2C_ADDR_TPS61050 0x0C
#define FLASH_I2C_ADDR_LM3554   0x53

#if BIND_FLASH_TPS61050
#define FLASH_I2C_ADDR          FLASH_I2C_ADDR_TPS61050
#endif
#if BIND_FLASH_LM3554
#define FLASH_I2C_ADDR          FLASH_I2C_ADDR_LM3554
#endif


MMPF_I2CM_ATTRIBUTE m_flash_I2cmAttribute = {
            SENSOR_I2CM_ID, FLASH_I2C_ADDR, 8, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE,
            MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};  

extern volatile MMP_UBYTE   gbDramID;

MMP_ERR  MMPF_FLASH_WriteI2C(MMP_USHORT usAddr, MMP_USHORT usData)
{
	if((gbDramID == MMPF_DRAMID_WINBOND_SDR16) || (gbDramID == MMPF_DRAMID_WINBOND_SDR16_1_8V)){
        m_flash_I2cmAttribute.uI2cmID = MMPF_I2CM_ID_0;  // for AIT8433 70 pin package
    }
    else{
        m_flash_I2cmAttribute.uI2cmID = MMPF_I2CM_ID_1;  // for AIT8451 105 pin package
    }
	MMPF_I2cm_WriteReg(&m_flash_I2cmAttribute, usAddr, usData);
	return  MMP_ERR_NONE;
}

MMP_ERR  MMPF_FLASH_ReadI2C(MMP_USHORT usAddr, MMP_USHORT *usData)
{
	*usData = 0xFF;
	if((gbDramID == MMPF_DRAMID_WINBOND_SDR16) || (gbDramID == MMPF_DRAMID_WINBOND_SDR16_1_8V)){
        m_flash_I2cmAttribute.uI2cmID = MMPF_I2CM_ID_0;  // for AIT8433 70 pin package
    }
    else{
        m_flash_I2cmAttribute.uI2cmID = MMPF_I2CM_ID_1;  // for AIT8451 105 pin package
    }
    MMPF_I2cm_ReadReg(&m_flash_I2cmAttribute, usAddr, usData);
	
	return  MMP_ERR_NONE;
}

VENUS_FLASH_CONFIG gFlashConfig;

/*************************************************************************************************/
/* Venus Flash API                                                                               */
/*************************************************************************************************/
void VR_Flash_Ctl(ISP_FLASH_CTL ctl, ISP_FLASH_TYPE type)
{
	// if flash light is controlled by host, just return.
	if (gFlashConfig.SelfControl == MMP_FALSE) return;

	if (ctl == ISP_FLASH_CTL_EN && gFlashConfig.Mode != ISP_FLASH_MODE_OFF) {
		if (type == ISP_FLASH_TYPE_CAMERA) {
			switch (gFlashConfig.Mode) {
			case ISP_FLASH_MODE_CAMERA_FORCE_ON:
				VR_Flash_SetFlashModeOn();
				break;

			case ISP_FLASH_MODE_CAMERA_AUTO_ON:
				if (ISP_IF_AE_GetLightCond() <= gFlashConfig.LowLuxTh) {
					VR_Flash_SetFlashModeOn();
					gFlashConfig.Mode = ISP_FLASH_MODE_CAMERA_FORCE_ON;
				} else {
					gFlashConfig.Mode = ISP_FLASH_MODE_OFF;
				}
				break;
			}
		} else { // ISP_FLASH_TYPE_VIDEO
			switch (gFlashConfig.Mode) {
			case ISP_FLASH_MODE_VIDEO_FORCE_ON:
				VR_Flash_SetTorchModeOn();
				break;

			case ISP_FLASH_MODE_VIDEO_AUTO_ON:
				if (ISP_IF_AE_GetLightCond() <= gFlashConfig.LowLuxTh) {
					VR_Flash_SetTorchModeOn();
				}
				break;
			}
		}
	} else {
		VR_Flash_SetFlashModeOff();
		VR_Flash_SetTorchModeOff();
	}
}

void VR_Flash_SetTorchLevel(MMP_UBYTE level)
{
	gFlashConfig.TorchLevel = level;
}

void VR_Flash_SetCameraLevel(MMP_UBYTE level)
{
	gFlashConfig.CameraLevel = level;
}

void VR_Flash_SetMode(ISP_FLASH_MODE mode)
{
	gFlashConfig.Mode = mode;
}


/*************************************************************************************************/
/* Venus Flash Driver                                                                            */
/*************************************************************************************************/
void VR_Flash_Init(void)
{
#if BIND_FLASH_LM3554
	FLASH_LM3554_Init();
#endif

	// default flash status is off
	gFlashConfig.Status = ISP_FLASH_STATUS_OFF;

	// default flash mode is off
	gFlashConfig.Mode = ISP_FLASH_MODE_OFF;

	// default flash level for torch mode is 100%
	gFlashConfig.TorchLevel = FLASH_TORCH_MODE_MAX_LEVEL;

	// default flash level for camera mode is 100%
	gFlashConfig.CameraLevel = FLASH_CAMERA_MODE_MAX_LEVEL;

	// set default low lux threshold for auto flash mode
	gFlashConfig.LowLuxTh = FLASH_DEFAULT_LOW_LUX_TH;

	// get flash light self control switch according to projects
	gFlashConfig.SelfControl = MMP_TRUE;

	// set real flash
	VR_Flash_Ctl(ISP_FLASH_CTL_DIS, ISP_FLASH_TYPE_CAMERA);
	VR_Flash_Ctl(ISP_FLASH_CTL_DIS, ISP_FLASH_TYPE_VIDEO);
}

void VR_Flash_SetFlashModeOn(void)
{
#if BIND_FLASH_LM3554
	FLASH_LM3554_FlashModeOn(gFlashConfig.CameraLevel);
#endif
}

void VR_Flash_SetFlashModeOff(void)
{
#if BIND_FLASH_LM3554
	FLASH_LM3554_FlashModeOff();
#endif
}

void VR_Flash_SetTorchModeOn(void)
{
#if BIND_FLASH_LM3554
	FLASH_LM3554_TorchModeOn(gFlashConfig.TorchLevel);
#endif
}

void VR_Flash_SetTorchModeOff(void)
{
#if BIND_FLASH_LM3554
	FLASH_LM3554_TorchModeOff();
#endif
}

#endif
